package com.java110.mall.cart.smo.impl;


import com.alibaba.fastjson.JSONObject;
import com.java110.intf.acct.ICouponShopPoolDetailV1InnerServiceSMO;
import com.java110.intf.acct.ICouponShopPoolV1InnerServiceSMO;
import com.java110.mall.cart.dao.IStoreOrderServiceDao;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.PageDto;
import com.java110.dto.coupon.CouponConfDto;
import com.java110.dto.coupon.CouponShopPoolDto;
import com.java110.dto.vipUser.ShopVipLevelDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderUserRoleDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.ICouponUserV1InnerServiceSMO;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.shop.ICouponConfInnerServiceSMO;
import com.java110.intf.shop.IShopVipLevelInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.cart.storeOrderEngine.StoreOrderShareAcct;
import com.java110.po.coupon.CouponShopPoolPo;
import com.java110.po.coupon.CouponShopPoolDetailPo;
import com.java110.po.coupon.CouponUserPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FloorInnerServiceSMOImpl
 * @Description 购物车内部服务实现类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class StoreOrderInnerServiceSMOImpl extends BaseServiceSMO implements IStoreOrderInnerServiceSMO {

    @Autowired
    private IStoreOrderServiceDao storeOrderServiceDaoImpl;

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;

    @Autowired
    private IShopVipLevelInnerServiceSMO shopVipLevelInnerServiceSMOImpl;
    @Autowired
    private ICouponConfInnerServiceSMO couponConfInnerServiceSMOImpl;
    @Autowired
    private ICouponUserV1InnerServiceSMO couponUserV1InnerServiceSMOImpl;
    @Autowired
    private ICouponShopPoolDetailV1InnerServiceSMO couponShopPoolDetailV1InnerServiceSMOImpl;
    @Autowired
    private ICouponShopPoolV1InnerServiceSMO couponShopPoolV1InnerServiceSMOImpl;
    @Autowired
    private StoreOrderShareAcct storeOrderShareAcct;
    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

//    @Autowired
//    private IOwnerV1InnerServiceSMO ownerV1InnerServiceSMOImpl;

    @Override
    public int saveStoreOrder(@RequestBody StoreOrderPo storeOrderPo) {
        int saveFlag = 1;
        storeOrderServiceDaoImpl.saveStoreOrderInfo(BeanConvertUtil.beanCovertMap(storeOrderPo));
        return saveFlag;
    }

    @Override
    public int updateStoreOrder(@RequestBody StoreOrderPo storeOrderPo) {
        int saveFlag = 1;
        storeOrderServiceDaoImpl.updateStoreOrderInfo(BeanConvertUtil.beanCovertMap(storeOrderPo));
        return saveFlag;
    }

    @Override
    public int deleteStoreOrder(@RequestBody StoreOrderPo storeOrderPo) {
        int saveFlag = 1;
        storeOrderPo.setStatusCd("1");
        storeOrderServiceDaoImpl.updateStoreOrderInfo(BeanConvertUtil.beanCovertMap(storeOrderPo));
        return saveFlag;
    }

    @Override
    public List<StoreOrderDto> queryOnlyStoreOrders(@RequestBody StoreOrderDto storeOrderDto) {

        //校验是否传了 分页信息

        int page = storeOrderDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            storeOrderDto.setPage((page - 1) * storeOrderDto.getRow());
        }

        List<StoreOrderDto> storeOrders = BeanConvertUtil.covertBeanList(storeOrderServiceDaoImpl.getOnlyStoreOrderInfo(BeanConvertUtil.beanCovertMap(storeOrderDto)), StoreOrderDto.class);

        return storeOrders;
    }

    @Override
    public List<StoreOrderDto> queryStoreOrders(@RequestBody StoreOrderDto storeOrderDto) {

        //校验是否传了 分页信息

        int page = storeOrderDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            storeOrderDto.setPage((page - 1) * storeOrderDto.getRow());
        }

        List<StoreOrderDto> storeOrders = BeanConvertUtil.covertBeanList(storeOrderServiceDaoImpl.getStoreOrderInfo(BeanConvertUtil.beanCovertMap(storeOrderDto)), StoreOrderDto.class);

        return storeOrders;
    }

    @Override
    public int saveOrderGoldCount(@RequestBody StoreOrderCartDto storeOrderCartDto) {

        //double amount = Double.parseDouble(storeOrder.getString("payPrice"));
        //vipuser.getString("vipId")
        //查询会员是否存在
        int detaFlag = 0;
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(storeOrderCartDto.getShopId());
        shopVipUserDto.setVipId(storeOrderCartDto.getVipId());
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        Assert.listOnlyOne(shopVipUserDtos, "会员不存在");

        ShopVipLevelDto shopVipLevelDto = new ShopVipLevelDto();
        shopVipLevelDto.setShopId(storeOrderCartDto.getShopId());
        shopVipLevelDto.setVipLevelId(shopVipUserDtos.get(0).getVipLevel());
        List<ShopVipLevelDto> shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
        Assert.listOnlyOne(shopVipLevelDtos, "会员等级不存在");

        CouponConfDto couponConfDto = new CouponConfDto();
        couponConfDto.setShopId(shopVipLevelDtos.get(0).getShopId());
        couponConfDto.setVipLevelId(shopVipLevelDtos.get(0).getVipLevelId());
        List<CouponConfDto> couponConfDtos = couponConfInnerServiceSMOImpl.queryCouponConfs(couponConfDto);


        double goldCostMoney = Double.parseDouble(shopVipLevelDtos.get(0).getGoldCostMoney());

        //支付金额除以钱 送基数
        double goldCount = Math.floor(Double.parseDouble(storeOrderCartDto.getPayPrice()) / goldCostMoney);
        if (0 == goldCount) {
            return detaFlag;
        }
        //会员等级未关联优惠券
        if (couponConfDtos == null || couponConfDtos.size() < 1) {
            return detaFlag;
        }
        for (CouponConfDto couponConf : couponConfDtos) {
            int runNumber = 0;
            int tmpStock = 0;
            BigDecimal tempcouponCount = new BigDecimal(couponConf.getCouponCount());
            //送多少劵
            BigDecimal price = tempcouponCount.multiply(new BigDecimal(goldCount)).setScale(0, BigDecimal.ROUND_DOWN);
            //查询店铺所拥有的购物券，验证库存与扣除购物券关系
            CouponShopPoolDto couponShopPoolDto = new CouponShopPoolDto();
            couponShopPoolDto.setPoolId(couponConf.getPoolId());
            couponShopPoolDto.setShopId(couponConf.getShopId());
            couponShopPoolDto.setSpId(couponConf.getSpId());
            List<CouponShopPoolDto> couponShopPoolDtos = couponShopPoolV1InnerServiceSMOImpl.queryCouponShopPools(couponShopPoolDto);
            BigDecimal stock = new BigDecimal(couponShopPoolDtos.get(0).getStock());
            //库存为0的情况下直接返回不做购物券处理
            if (0 == stock.intValue()) {
                continue;
            }
            //校验库存与所需扣除数量，相等或者库存不够，库存有多少送多少，
            if ((stock.subtract(price).setScale(0, BigDecimal.ROUND_DOWN).doubleValue()) <= 0) {
                runNumber = stock.intValue();
            } else {
                runNumber = price.intValue();
            }
            //根据数量 按照一个购物券为一条数据，为用户写入购物券明细
            for (int userIndex = 0; userIndex < runNumber; userIndex++) {
                CouponUserPo couponUserPo = new CouponUserPo();
                couponUserPo.setCouponId(GenerateCodeFactory.getGeneratorId("11"));
                couponUserPo.setCouponName(couponShopPoolDtos.get(0).getCouponName());
                couponUserPo.setActualPrice(couponShopPoolDtos.get(0).getActualPrice());
                couponUserPo.setPoolId(couponShopPoolDtos.get(0).getPoolId());
                couponUserPo.setValidityDay(couponShopPoolDtos.get(0).getValidityDay());
                couponUserPo.setUserId(shopVipUserDtos.get(0).getVipId());
                couponUserPo.setUserName(shopVipUserDtos.get(0).getVipName());
                couponUserPo.setTel(shopVipUserDtos.get(0).getVipTel());
                couponUserPo.setState(CouponUserPo.STATE_RUN);
                detaFlag = couponUserV1InnerServiceSMOImpl.saveCouponUser(couponUserPo);
                if (detaFlag < 1) {
                    throw new IllegalArgumentException("购物券写入用户信息表失败");
                }
            }
            if ((stock.subtract(price).setScale(0, BigDecimal.ROUND_DOWN).doubleValue()) <= 0) {
                tmpStock = 0;
            } else {
                tmpStock = stock.subtract(price).setScale(0, BigDecimal.ROUND_DOWN).intValue();
            }
            //根据赠送数量刷新店铺购物券库存数据
            CouponShopPoolPo couponShopPoolPo = new CouponShopPoolPo();
            couponShopPoolPo.setSpId(couponShopPoolDtos.get(0).getSpId());
            couponShopPoolPo.setStock(tmpStock + "");
            detaFlag = couponShopPoolV1InnerServiceSMOImpl.updateCouponShopPool(couponShopPoolPo);
            if (detaFlag < 1) {
                throw new IllegalArgumentException("店铺购物券刷新库存失败");
            }
            //记录赠送明细表
            CouponShopPoolDetailPo couponShopPoolDetailPo = new CouponShopPoolDetailPo();
            couponShopPoolDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId("11"));
            couponShopPoolDetailPo.setCouponName(couponShopPoolDtos.get(0).getCouponName());
            couponShopPoolDetailPo.setPoolId(couponShopPoolDtos.get(0).getPoolId());
            couponShopPoolDetailPo.setShopId(couponShopPoolDtos.get(0).getShopId());
            couponShopPoolDetailPo.setSpId(couponShopPoolDtos.get(0).getSpId());
            couponShopPoolDetailPo.setActualPrice(couponShopPoolDtos.get(0).getActualPrice());
            couponShopPoolDetailPo.setSendCount(runNumber + "");
            couponShopPoolDetailPo.setValidityDay(couponShopPoolDtos.get(0).getValidityDay());
            couponShopPoolDetailPo.setOrderId(storeOrderCartDto.getOrderId());
            couponShopPoolDetailPo.setUserId(shopVipUserDtos.get(0).getVipId());
            couponShopPoolDetailPo.setUserName(shopVipUserDtos.get(0).getVipName());
            couponShopPoolDetailPo.setTel(shopVipUserDtos.get(0).getVipTel());
            detaFlag = couponShopPoolDetailV1InnerServiceSMOImpl.saveCouponShopPoolDetail(couponShopPoolDetailPo);
            if (detaFlag < 1) {
                throw new IllegalArgumentException("记录店铺购物券赠送明细失败");
            }
        }

        return detaFlag;
    }


    @Override
    public int queryStoreOrdersCount(@RequestBody StoreOrderDto storeOrderDto) {
        return storeOrderServiceDaoImpl.queryStoreOrdersCount(BeanConvertUtil.beanCovertMap(storeOrderDto));
    }

    @Override
    public StoreOrderUserRoleDto queryStoreOrderUser(@RequestBody StoreOrderUserRoleDto storeOrderUserDto) {
        StoreOrderUserRoleDto tmpStoreOrderUserDto = null;

        UserDto userDto = new UserDto();
        userDto.setUserId(storeOrderUserDto.getUserId());
        List<UserDto> userDtoList = userV1InnerServiceSMOImpl.queryUsers(userDto);
        if (userDtoList == null || userDtoList.size() < 1) {
            return null;
        }
        tmpStoreOrderUserDto = BeanConvertUtil.covertBean(userDtoList.get(0), StoreOrderUserRoleDto.class);

        //查询店铺会员是否存在 如果存在 不添加 add by wuxw
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(storeOrderUserDto.getShopId());
        shopVipUserDto.setVipTel(userDtoList.get(0).getTel());
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        if (shopVipUserDtos != null && shopVipUserDtos.size() > 0) {
            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_VIP);
            return tmpStoreOrderUserDto;
        }

        //查询 是否业主
//        OwnerDto ownerDto = new OwnerDto();
//        ownerDto.setLink(userDtoList.get(0).getTel());
//        ownerDto.setPage(1);
//        ownerDto.setRow(1);
//        List<OwnerDto> ownerDtos = ownerV1InnerServiceSMOImpl.queryOwners(ownerDto);
//        if (ownerDtos != null && ownerDtos.size() > 0) {
//            tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_OWNER);
//            return tmpStoreOrderUserDto;
//        }

        tmpStoreOrderUserDto.setRole(StoreOrderUserRoleDto.ROLE_COMMON);
        return tmpStoreOrderUserDto;
    }

    @Override
    public int queryReportStoreOrderCount(@RequestBody StoreOrderDto storeOrderDto) {
        return storeOrderServiceDaoImpl.queryReportStoreOrderCount(BeanConvertUtil.beanCovertMap(storeOrderDto));
    }

    @Override
    public double queryReportStoreOrderMoney(@RequestBody StoreOrderDto storeOrderDto) {
        return storeOrderServiceDaoImpl.queryReportStoreOrderMoney(BeanConvertUtil.beanCovertMap(storeOrderDto));
    }

    @Override
    public List<Map> queryStoreOrderDayCount(@RequestBody JSONObject reqJson) {
        return storeOrderServiceDaoImpl.queryStoreOrderDayCount(reqJson);
    }

    @Override
    public int shareAcct(@RequestBody StoreOrderDto storeOrderDto) {
        storeOrderShareAcct.shareAcct(storeOrderDto);
        return 1;
    }

    public IStoreOrderServiceDao getStoreOrderServiceDaoImpl() {
        return storeOrderServiceDaoImpl;
    }

    public void setStoreOrderServiceDaoImpl(IStoreOrderServiceDao storeOrderServiceDaoImpl) {
        this.storeOrderServiceDaoImpl = storeOrderServiceDaoImpl;
    }
}
